<?php

class IndexController extends BaseController {
	var $pageSize = 200;
	var $maxNum = 2000;
	
	/*
	 * controller 魔术方法，此方法不可少
	 */
	function __construct() {
		parent::__construct();

		if(!isset($_SESSION['user'])) {
			$this->redirect('/member/index');
			die;
		}
	}

	function actionCreate($get) {
		$serial = KJ::M('Serialnumber');
		$pageData = array();

		if(isset($_POST['serial']) && !empty($_POST)) {
			$firstWord = $serial->validFirst($_POST['serial']['first']);
			if($firstWord === false) {
				$pageData['error'] = '请填写正确的首位序列号';
				goto end;
			}

			$year = $serial->getYear($_POST['serial']['year']);
			if($year ===  false) {
				$pageData['error'] = '请填写正确的年份';
				goto end;
			}

			$total = $serial->getTotal($_POST['serial']['total']);
			if($total ===  false) {
				$pageData['error'] = '请填写正确的生成总量';
				goto end;
			}

			if($total > $this->maxNum) {
				$pageData['error'] = '生成总量一次不可超过'.$this->maxNum;
				goto end;	
			}

			$batches = $this->_createSerial($serial, $firstWord, $year, $total);
			$pageData['batches'] = $batches;

			$pageData['list'] = $serial->where('batches = "'.$batches.'"')->selectAll();
		}

		end:

		$this->assign($pageData);
		$this->display();
	}

	function actionIndex($get) {
		$pageData = [];
		$page = (isset($get['page']) && is_numeric($get['page']) && $get['page'] > 1) ? $get['page'] : 1;
		$start = ($page-1) * $this->pageSize;

		$serial = KJ::M('Serialnumber');

		$where = $this->_filterWhere($get);
		$serialTotoal = $serial->where($where)->count();
		$list = $serial->where($where)->limit($start.', '.$this->pageSize)->select();

		$pages = pages($serialTotoal, $this->pageSize);

		$pageData['list'] = $list;
		$pageData['pages'] = $pages;

		$this->assign($pageData);
		$this->display();
	}

	private function _filterWhere($params) {
		$where = ' 1=1 ';

		if(isset($params['search'])) {
			$keyword = $params['search']['keyword'];
			$year = $params['search']['year'];
			$status = $params['search']['status'];
			$create_start = $params['search']['create_start'];
			$create_end = $params['search']['create_end'];

			if($keyword) {
				$where .= ' AND `xuliehao` LIKE "%'.$keyword.'%"';
			}

			if($year) {
				$where .= ' AND `year` = "'.$year.'"';
			}

			if(is_numeric($status)) {
				$where .= ' AND `status` = "'.$status.'"';
			}

			if($create_start) {
				$where .= ' AND `created_at` > "'.$create_start.'"';
			}

			if($create_end) {
				$where .= ' AND `created_at` < "'.$create_end.'"';
			}
		}

		return $where;
	}

	private function _createSerial($serial, $firstWord, $year, $total) {
		set_time_limit(240);
		$batches = $serial->getBatches();
		$created_at = date('Y-m-d H:i:s');

		$inserData = array(
			'year' => $year,
			'batches' => $batches,
			'created_at' => $created_at, 
			'updated_at' => $created_at,
		);

		for ($i=0; $i <$total ; $i++) { 
			$firstCode = $serial->firstCode($firstWord);
			$inserData['xuliehao'] = $serial->fullSerial($firstCode);

			$serial->insert($inserData);
		}

		return $batches;
	}
}